﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using WindowsPhone.Tools;
using Microsoft.SmartDevice.Connectivity;
using System.ComponentModel;

namespace EzDeploy
{
    public class ListedXaps : ObservableCollection<Xap>
    {
        public bool IsWillStop = false;

        RemoteApplication existingInstall;

        private string _statusMessage;
        public string StatusMessage
        {
            get { return _statusMessage; }
            set
            {
                if (_statusMessage != value)
                {
                    _statusMessage = value;

                    NotifyPropertyChanged("StatusMessage");
                }
            }
        }

        private Device _currentDevice;
        public Device CurrentDevice
        {
            get { return _currentDevice; }
            set
            {
                if (_currentDevice != value)
                {
                    _currentDevice = value;
                }
            }
        }

        private WindowsPhoneDevice _device;

        public ListedXaps(WindowsPhoneDevice device)
        {
            _device = device;
        }

        public ListedXaps()
        {
        }

        public void InstallSelectedXap(Xap _xap)
        {
            try
            {
                if (isXapExisted(_xap))
                {
                    StatusMessage = LocalizedStrings.str_updating + ": " + _xap.FileName;
                    existingInstall = _device.CurrentDevice.GetApplication(_xap.Guid);
                    existingInstall.UpdateApplication("genre", "noicon", _xap.FilePath);
                    StatusMessage = LocalizedStrings.str_updated + ": " + _xap.FileName;
                }
                else
                {
                    StatusMessage = LocalizedStrings.str_installing + ": " + _xap.FileName;
                    _device.CurrentDevice.InstallApplication(_xap.Guid, Guid.Empty, "genre", "noicon", _xap.FilePath);
                    StatusMessage = LocalizedStrings.str_installed + ": " + _xap.FileName;
                }
                _device.RefreshInstalledApps();
            }
            catch (Exception ex)
            {
                StatusMessage = LocalizedStrings.str_notinstalled + ": " + _xap.FileName;
                string msg = LocalizedStrings.str_Error;
                switch (ex.Message)
                {
                    case "0x81030111":
                        msg = LocalizedStrings.str_cause + ": " + _device.CurrentDevice.Name + " " + LocalizedStrings.str_usingapp;
                        break;
                    case "The device is not connected":
                        msg = LocalizedStrings.str_notconnect;
                        break;
                    default:
                        break;
                }
                StatusMessage = ex.Message + " " + msg;
            }
        }

        public void UninstallSelectedXap(Xap _xap)
        {
            try
            {
                if (isXapExisted(_xap))
                {
                    StatusMessage = LocalizedStrings.str_uninstalling + ": " + _xap.FileName;
                    existingInstall = _device.CurrentDevice.GetApplication(_xap.Guid);
                    existingInstall.Uninstall();
                    _device.RefreshInstalledApps();
                    StatusMessage=LocalizedStrings.str_uninstalled + ": " + _xap.FileName;
                }
                else
                {
                    StatusMessage = LocalizedStrings.str_notuninstalled + ": " + _xap.FileName + "  " + LocalizedStrings.str_appnotinstalled;
                }
            }
            catch (Exception ex)
            {
                StatusMessage = LocalizedStrings.str_notinstalled + ": " + _xap.FileName;
                string msg = LocalizedStrings.str_Error;
                switch (ex.Message)
                {
                    case "0x81030111":
                        msg = LocalizedStrings.str_cause + ": " + _device.CurrentDevice.Name + " " + LocalizedStrings.str_usingapp;
                        break;
                    case "The device is not connected":
                        msg = LocalizedStrings.str_notconnect;
                        break;
                    default:
                        break;
                }
                StatusMessage=ex.Message + " " + msg;
            } 
        }

        public void InstallListedXaps()
        {
            foreach (Xap item in base.Items)
            {
                if (!IsWillStop)
                {
                    InstallSelectedXap(item);
                }
                else
                {
                    StatusMessage = LocalizedStrings.str_operationstopped;
                    IsWillStop = true;
                    break;
                }
            }
        }

        private bool isXapExisted(Xap xap)
        {
            if (_device.CurrentDevice.IsApplicationInstalled(xap.Guid))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        # region INotifyPropertyChanged

        public new event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        #endregion
    }
}
